package com.m7788.screen;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;

import com.m7788.view.UmengBaseActivity;

public class GuideActivity extends UmengBaseActivity {
	private LinearLayout guide1, guide2, guide3, guide4;
	private ViewPager viewPager;
	private List<View> viewList;
	private PagerAdapter guidePagerAdapter = new PagerAdapter() {

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return (arg0 == arg1);
		}

		@Override
		public int getCount() {
			return viewList.size();
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(viewList.get(position));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(viewList.get(position));
			return viewList.get(position);
		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.guide);
		showModeCheck();
	}

	private void initActivity() {
		viewPager = (ViewPager) findViewById(R.id.pager);
		LayoutInflater inflater = LayoutInflater.from(this);
		guide1 = (LinearLayout) inflater.inflate(R.layout.guide1, null);
		guide2 = (LinearLayout) inflater.inflate(R.layout.guide2, null);
		guide3 = (LinearLayout) inflater.inflate(R.layout.guide3, null);
		guide4 = (LinearLayout) inflater.inflate(R.layout.guide4, null);

		viewList = new ArrayList<View>();
		viewList.add(guide1);
		viewList.add(guide2);
		viewList.add(guide3);
		viewList.add(guide4);

		viewPager.setAdapter(guidePagerAdapter);
		guide4.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(GuideActivity.this, IndexScreen.class);
				overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
				startActivity(intent);
				finish();
			}
		});
	}

	private void showModeCheck() {
		SharedPreferences mSharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(this);
		if (mSharedPreferences.getString("isFirstUse", "true").equals("true")) {
			initActivity();
			SharedPreferences.Editor editor = mSharedPreferences.edit();
			editor.putString("isFirstUse", "false");
			editor.commit();
		} else {
			Intent intent = new Intent(this, IndexScreen.class);
			overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
			startActivity(intent);
			this.finish();
		}
	}

}